import {
    getBill,
    exportBill,
    exportPeriodBill
} from '../../api/long-insure'
import dayjs from 'dayjs'
import {
    dateFormat,
    openDocument
} from '../../../utils/util'
const app = getApp()
Page({
    data: {
        date: '',
        setIndex: 0,
        activityArr: [{
            id: 0,
            label: '每月被保人员账单'
        },
        {
            id: 1,
            label: '每日方案账单'
        },
        {
            id: 2,
            label: '每日被保人员账单'
        }
        ],
        isup: false,
        month: '',
        billList: [],
        total: '',
        id: ''
    },
    bindChange(e) {
        console.log(e, 'udiughiudf')
        this.data.date = e.detail.date
    },
    bindDateChange(e) {
        console.log(e)
        this.setData({
            date: e.detail.value
        })
    },
    bindPickerChange(e) {
        this.setData({
            setIndex: parseInt(e.currentTarget.dataset.i)
        })
    },
    onLoad(options) {
        if (options.id) {
            this.setData({
                id: options.id
            })
        }
        this.setData({
            month: dateFormat('YYYY-mm', new Date())
        })
        this.getBill()
    },
    onCancelDialog() {
        this.setData({
            isup: false
        })
    },
    onMonth(event) {
        this.setData({
            month: event.detail.value
        })
        this.getBill()
    },
    async getBill() {
        const result = await getBill({
            // companyPlanId: this.data.id,
            queryMonth: this.data.month
        })
        this.setData({
            billList: result.list,
            total: result.totalMoney
        })
    },
    async onExportSure() {
        console.log(111)
        if (this.data.setIndex === '') {
            wx.showToast({
                title: '请选择账单类型',
                icon: 'none'
            })
        } else if (this.data.date === '') {
            wx.showToast({
                title: '请选择账单时间',
                icon: 'none'
            })
        } else {
            if (this.data.setIndex !== 2) {
                const file = await exportBill({
                    queryMonth: this.data.date,
                    queryType: this.data.setIndex,
                    companyPlanId: this.data.id
                })
                const url = app.globalData.baseUrl + '/common/download?fileName=' + file +
                    '&delete=false'
                openDocument(url, file)
            } else {
                const file = await exportPeriodBill({
                    effectiveDate: this.data.date,
                })
                const url = app.globalData.baseUrl + '/common/download?fileName=' + file +
                    '&delete=false'
                openDocument(url, file)
            }
        }
    },
    async onExport() {
        this.setData({
            isup: true,
            setIndex: 0
        })
    }
})